/**
 * Created by Administrator on 2018/1/18.
 */

layui.use(['jquery', 'laydate','form','layer','laypage','table'], function(){
    var $ = layui.jquery,
        laydate = layui.laydate,
        layer = layui.layer,
        table = layui.table,
        form = layui.form;

    laydate.render({
        elem: '#start'//指定元素
    });

    laydate.render({
        elem: '#end' //指定元素

    });

    var under_config = {
        elem: '#user',
        url:"/user/get_list_data",
        method:'post',
        cellMinWidth:80,
        cols:  [[
            //标题栏
            {field: 'id', title: '用户ID',align: 'center'},
            {field: 'firm_id', title: '交易商编号', edit:'text' ,align: 'center'},
            {field: 'name', title: '客户姓名',align: 'center'},
            {field: 'account_type', title: '客户类型',align: 'center'},
            {field: 'int_id', title: '推荐人',align: 'center'},
            {field: 'phone', title: '手机号码',align: 'center'},
            {field: 'register_time', title: '注册时间',align: 'center'},
            // {field: 'remark', title: '备注',align: 'center',width: 150},
            {fixed: 'right', align:'center', title: '操作',toolbar: '#barDemo'}
        ]],
        //data:data.data,
        page: true,
        limit: 15,
        height: 680,
        // size:'sm',
        done: function(res, curr, count){
            layer.closeAll();
            $('#int_id').html(res.extra.user_option);
            form.render('select');
        }
    };

    //监听表格工具条
    table.on('tool(user)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(layEvent === 'detail'){ //查看
            var id = data.id;
            var open_url = '/user/detail?id='+id;
            window.open(open_url);
        }
    });

    //监听单元格编辑
    table.on('edit(user)', function(obj){
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段

        alert('123');

        console.log(obj.data);
        return false;

        $.ajax({
            type: "POST",
            data: {
                'id':data.id,
                'field':obj.value,
            },
            cache:false,
            url: "/user/update_info",
            dataType: "json",
            success: function(data){

                if(data.code == 200){
                    layer.msg(data.msg);
                    return false;
                }else{
                    //返回错误信息
                    var message = '';
                    $.each(data.data, function(idx, obj) {
                        message += obj+"<br/>";
                    });

                    layer.open({
                        content: message,
                        skin: 'msg',
                        time: 3 //2秒后自动关闭
                    });
                    return false;
                }
            },
            error:function(data){
                return false;
            }
        });

            layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
    });

    //方法级渲染-默认显示入金记录
    var ins1 = table.render(under_config);

    //表格搜索和重载
    var $ = layui.$,
        active = {
            reload: function(){
                var index = layer.msg('数据加载中,请耐心等待...', {
                    icon: 16,shade: 0.5,time:15000
                });

                var int_id = $('#int_id').val();
                var start_time = $('#start').val();
                var end_time = $('#end').val();
                var keyword = $('#keyword').val();

                ins1.reload({
                    where: {
                        //根据MT4搜索
                        int_id:int_id,
                        status:status,
                        keyword:keyword,
                        start_time:start_time,
                        end_time:end_time
                    }
                });
            },
            export:function(){

                var int_id = $('#int_id').val();
                var start_time = $('#start').val();
                var end_time = $('#end').val();
                var keyword = $('#keyword').val();

                window.location.href = '/user/export?int_id='+int_id+'&keyword='+keyword+'&start_time='+start_time+'&end_time='+end_time+'&rand_id='+new Date().getTime();
            }
        };

    $('.content-search .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

});
